#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/3/20 13:57
# @Author  : 柳晓飞
# @Site    : 
# @File    : gameServerHand.py
# @Software: PyCharm
import net.protocal as protocal
import yaml
import jjGlobal.jjGlobal as jjGlobal
import time
import datetime

class GameServerHand:
    def __init__(self):
        self.autoPlayerID = 100001
        self.registMsgDict = {}#key=msgID value=function[]
        self.playerClientDict = {}#key=playerID , value=client

    def onMsg(self,data,client):
        try:
            id = data['id']  # 协议号
            if id == protocal.login:
                self.login(data, client)
            elif id == protocal.tick:
                self.allLogicUpdate()
            else:
                playerID = client.playerID
                if not data.has_key('playerID'):
                    data['playerID'] = playerID
                self.dispatchMsg(data)
        except Exception, e:
            print('服务器收到如下处理不了的消息： ')
            print data


    def onClose(self,client):
        try:
            if self.playerClientDict.has_key(client.playerID):
                print '用户%d 断开连接'%client.playerID
                del self.playerClientDict[client.playerID]
                jjGlobal.gameserver.tableManager.leaveIdleList(client.playerID)
        except Exception, e:
            print '未登录用户断开连接'
    def isPlayerOnline(self,playerID):
        if self.playerClientDict.has_key(playerID):
            return True
        return False

    def sendMsg(self,playerID,msg):
        if self.playerClientDict.has_key(playerID):
            client = self.playerClientDict[playerID]
            client.ws.send(msg)

    def broadcast(self,msg):
        for client in self.playerClientDict.values():
            client.ws.send(msg)

    def login(self,data,client):
        client.playerID = self.autoPlayerID
        self.playerClientDict[self.autoPlayerID] = client
        res = {}
        res['id'] = protocal.login
        res['playerID'] = self.autoPlayerID
        client.ws.send(yaml.dump(res))
        self.autoPlayerID += 1

    def allLogicUpdate(self):
        tablesDict = jjGlobal.gameserver.tableManager.tablesDict
        for table in tablesDict.values():
            table.logic.update()

    def registMsg(self,msgID , obj,fuc):
        if not self.registMsgDict.has_key(msgID):
            self.registMsgDict[msgID] = []
        callBackDic = {}
        callBackDic['obj'] = obj
        callBackDic['fuc'] = fuc
        self.registMsgDict[msgID].append(callBackDic)

    def unregistMsg(self,msgID,fuc):
        if self.registMsgDict.has_key(msgID):
            callBackDicList = self.registMsgDict[msgID]
            for callBackDic in callBackDicList:
                callBackFuc = callBackDic['fuc']
                if callBackFuc == fuc:
                    callBackDicList.remove(callBackDic)
                    break
    def dispatchMsg(self,data):
        try:
            msgID = data['id']
            playerID = data['playerID']
            if self.registMsgDict.has_key(msgID):
                callBackDicList = self.registMsgDict[msgID]
                for callBackDic in callBackDicList:
                    callBackFunc = callBackDic['fuc']
                    callBackOjb = callBackDic['obj']
                    # print callBackOjb
                    # print callBackFunc
                    value = callBackOjb.isRecMsg(data)
                    if(value):
                        callBackFunc(data)
        except Exception, e:
            print '下面的消息派发失败'
            print data
